package com.hgyc.mom.wms.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.wms.mapper.WmsProductReturnMapper;
import com.hgyc.mom.wms.entity.WmsProductReturn;
import com.hgyc.mom.wms.entity.WmsProductReturnDetail;
import com.hgyc.mom.wms.service.WmsProductReturnDetailService;
import com.hgyc.mom.wms.vo.WmsProductReturnVO;
import com.hgyc.mom.wms.service.WmsProductReturnService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import com.hgyc.mom.common.exception.BizException;
import org.springframework.util.CollectionUtils;

/**
 * 生产退料单Service业务层实现
 *
 * @author fwj
 * @since 2025-07-09
 */
@Slf4j
@Service
public class WmsProductReturnServiceImpl extends ServiceImpl<WmsProductReturnMapper, WmsProductReturn> implements WmsProductReturnService {

    @Autowired
    private WmsProductReturnDetailService wmsProductReturnDetailService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveProductReturn(WmsProductReturnVO wmsProductReturnVO) {
        // TODO 其他逻辑
        save(wmsProductReturnVO);

        //新增生产退料单明细
        List<WmsProductReturnDetail> details = wmsProductReturnVO.getAddDetails();
        if (!CollectionUtils.isEmpty(details)) {
            details.forEach(item -> {
                item.setId(null);
                item.setReturnId(wmsProductReturnVO.getId());
            });
            wmsProductReturnDetailService.saveBatch(details);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProductReturn(WmsProductReturnVO wmsProductReturnVO) {
        // TODO 其他逻辑
        updateById(wmsProductReturnVO);

        //删除生产退料单明细
        if (!CollectionUtils.isEmpty(wmsProductReturnVO.getDeleteDetailIds())) {
            wmsProductReturnDetailService.removeBatchByIds(wmsProductReturnVO.getDeleteDetailIds());
        }

        //新增生产退料单明细
        List<WmsProductReturnDetail> details = wmsProductReturnVO.getAddDetails();
        if (!CollectionUtils.isEmpty(details)) {
            details.forEach(item -> {
                item.setId(null);
                item.setReturnId(wmsProductReturnVO.getId());
            });
            wmsProductReturnDetailService.saveBatch(details);
        }

        //更新生产退料单明细
        if (!CollectionUtils.isEmpty(wmsProductReturnVO.getUpdateDetails())) {
            List<WmsProductReturnDetail> updateDetails = wmsProductReturnVO.getUpdateDetails();
            wmsProductReturnDetailService.updateBatchById(updateDetails);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeProductReturn(List<Long> ids) {
        // TODO 其他逻辑
        removeByIds(ids);
        //删除生产退料单明细
        wmsProductReturnDetailService.deleteBatchByParentIds(ids);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<WmsProductReturn> wmsProductReturnList, boolean updateSupport) {
        if (wmsProductReturnList == null || wmsProductReturnList.isEmpty()) {
            throw new IllegalArgumentException("导入数据不能为空");
        }

        try {
            if (updateSupport) {
                // TODO 实现更新已有数据的导入逻辑
                return "更新导入成功，共导入 " + wmsProductReturnList.size() + " 条数据";
            } else {
                // TODO 实现不更新已有数据的导入逻辑
                return "新增导入成功，共导入 " + wmsProductReturnList.size() + " 条数据";
            }
        } catch (Exception e) {
            log.error("生产退料单导入失败", e);
            throw new BizException("生产退料单导入失败：" + e.getMessage());
        }
    }
}